#include"my_register.h"
#include"my_gui.h"
#include <string.h>

#define FONTAWESOME_SYMBOL_USER  "\xef\x80\x87"   /*f007*/
#define FONTAWESOME_SYMBOL_KEY   "\xef\x82\x84"   /*f084*/
LV_FONT_DECLARE(lv_font_source_han_sans_bold_14);

LV_FONT_DECLARE(user_fontawesome);  //图标库
LV_FONT_DECLARE(user_login_name_12);

//-------------------------------------------------------------------------
static void background_set_color(lv_color_t color);    //更改背景颜色
void lv_run_interface_demo(void);                      //运行界面
static void user_timer(void);
static void lv_messagebox_create(void);
static lv_obj_t * window_obj;             //登陆窗口容器
static lv_obj_t * user_name_input;        //用户名文本框
static lv_obj_t * user_name_keyboard;     //用户名键盘
static lv_obj_t * password_input;         //密码文本框
static lv_obj_t * password_keyboard;      //密码键盘
static lv_obj_t* bg_img;

static lv_obj_t * temp_label;
static lv_obj_t *led1 ;
static  lv_obj_t* msgbox ;
lv_anim_t beep_anim;
lv_anim_t gif_anim;

static lv_timer_t *my_timer;
//--------------------------------回调函数区域-----------------------------------------------------
/*用户文本框点击事件回调*/
static void textarea_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)  // 当文本区域获得焦点时显示键盘
    {
        lv_obj_clear_flag(user_name_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
    else if(code == LV_EVENT_DEFOCUSED)// 当文本区域失去焦点时隐藏键盘
    {
        lv_obj_add_flag(user_name_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
}
/*密码文本框点击事件回调*/
static void password_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)  // 当文本区域获得焦点时显示键盘
    {
        lv_obj_clear_flag(password_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
    else if(code == LV_EVENT_DEFOCUSED)// 当文本区域失去焦点时隐藏键盘
    {
        lv_obj_add_flag(password_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
}

/*键盘按钮√的事件回调*/
static void keyboard_event_cb(lv_event_t * e)
{
     lv_event_code_t code = lv_event_get_code(e);
     if(code == LV_EVENT_READY) //当按下键盘的√按键 触发隐藏键盘
     {
        lv_obj_add_flag(user_name_keyboard, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(password_keyboard, LV_OBJ_FLAG_HIDDEN);
     }
}
/*登陆事件回调函数*/
static void login_btn_event_cb(lv_event_t*e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
    {
        const char *user_txt = lv_textarea_get_text(user_name_input);       //获取输入的账户名
        const char *password_txt = lv_textarea_get_text(password_input);    //获取输入的账户密码

        printf("%s:%s\n",user_txt,password_txt);
        lv_messagebox_create(); //建立消息框
        user_create();                                            
        user_load(head1,USER_FILE);
        if(user_login(head1,user_txt,password_txt) == 1)
        {
             printf("login success!\n");
             lv_obj_del(window_obj);                                          //删除登入窗口对象
             lv_obj_del(msgbox);                                              //密码成功,删除消息框
             background_set_color(lv_color_hex(0xffffff));                    //设置背景颜色白色
             custom_demo_jm();                                                  //系统运行顾客点单界面
        }
        else if (strncmp("admin",user_txt,sizeof(user_txt))==0 && strncmp("123",password_txt,sizeof(password_txt))==0)
        {
            printf("login admin system success!\n");
             lv_obj_del(window_obj);                                          //删除登入窗口对象
             lv_obj_del(msgbox);                                              //密码成功,删除消息框
             background_set_color(lv_color_hex(0xff0000));                    //设置背景颜色白色
             admini_jm();
        }
        
        else
        {
            printf("login fail!\n");
            lv_obj_clear_flag(msgbox,LV_OBJ_FLAG_HIDDEN);  //密码错误,显示消息框
        }

    }
}

// 注册按钮事件的回调函数
static void register_btn_event_cb(lv_event_t*e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
    {
             printf("Please Register!\n");
             lv_obj_del(window_obj);
             my_register_ui();                                              //系统运行界面
    }
}
static void admin_btn_event_cb(lv_event_t*e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
    {
             printf("Login Administrator!\n");
             background_set_color(lv_color_hex(0x00ff00));                    //设置背景颜色绿
             lv_admin_demo();                                                 //系统运行界面
    }
}

//--------------------------------主函数区域-----------------------------------------------------
/*背景颜色设置函数*/
static void background_set_color(lv_color_t color)
{
    lv_obj_t * scr = lv_scr_act();              //获取屏幕
    lv_obj_set_style_bg_color(scr,color, 0);    //设置背景颜色
}
static void background_img(void)
{
    bg_img = lv_img_create(lv_scr_act());
	LV_IMG_DECLARE(custom_p); //声明图片
    lv_img_set_src(bg_img, &custom_p); //设置图片源
}
// 
static void lv_login_window_demo(void)
{
    background_img();

    // background_set_color(lv_color_hex(0x1E90FF));  //设置背景颜色蓝色

    /*创建一个容器*/
    window_obj = lv_obj_create(lv_scr_act());
    lv_obj_set_size(window_obj,lv_obj_get_width(lv_scr_act())/2,400);
    lv_obj_set_style_bg_opa(window_obj,100,0);
    lv_obj_set_style_border_opa(window_obj,0,0);
    lv_obj_center(window_obj);

    /*创建一个父类 占位对象1，优化布局用*/
    lv_obj_t * name_obj = lv_obj_create(window_obj);
    lv_obj_set_size(name_obj, 250, 44);
    lv_obj_align(name_obj, LV_ALIGN_TOP_MID, 0, 20);
    lv_obj_set_scrollbar_mode(name_obj, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能

    /* 创建用户名输入框 */
    user_name_input = lv_textarea_create(window_obj);
    lv_textarea_set_placeholder_text(user_name_input, "name"); //提示文本
    lv_obj_set_size(user_name_input, 210, 44);
    lv_textarea_set_one_line(user_name_input, true);                    //限制为1行高度
    lv_textarea_set_max_length(user_name_input,6);                      //限制长度为5个字符
    lv_obj_set_scrollbar_mode(user_name_input, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能
    lv_obj_set_style_border_opa(user_name_input,0,0);                   //边框透明为0
    lv_obj_align(user_name_input, LV_ALIGN_TOP_MID, 20, 20);
    lv_obj_add_event_cb(user_name_input, textarea_event_cb, LV_EVENT_ALL, NULL); //用户名输入框回调
    /*创建用户名键盘*/
    user_name_keyboard =lv_keyboard_create(lv_scr_act());
    lv_obj_set_size(user_name_keyboard,600,200);
    lv_obj_set_style_radius(user_name_keyboard,20,0);
    lv_keyboard_set_textarea(user_name_keyboard,user_name_input);
    lv_obj_add_event_cb(user_name_keyboard, keyboard_event_cb, LV_EVENT_READY, NULL);
    lv_obj_add_flag(user_name_keyboard,LV_OBJ_FLAG_HIDDEN);//默认隐藏
    /*用户图标对象框*/
    lv_obj_t *user_name_obj = lv_obj_create(window_obj);
    lv_obj_set_size(user_name_obj,44,44);
    lv_obj_set_style_bg_color(user_name_obj,lv_color_hex(0x696969),0);
    lv_obj_align_to(user_name_obj,user_name_input,LV_ALIGN_OUT_LEFT_MID,0,0);
    lv_obj_set_scrollbar_mode(user_name_obj, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能
    /*用户图标对象*/
    lv_obj_t * user_name_label = lv_label_create(window_obj);
    lv_obj_set_style_text_font(user_name_label,&user_fontawesome,0);
    lv_label_set_text(user_name_label, FONTAWESOME_SYMBOL_USER);
    lv_obj_set_style_text_color(user_name_label,lv_color_hex(0xffffff),0);
    lv_obj_align_to(user_name_label,user_name_obj,LV_ALIGN_CENTER,0,0);
//--------------------------------------------------------------------------
    /*创建一个父类 占位对象2，优化布局用*/
    lv_obj_t * key_obj = lv_obj_create(window_obj);
    lv_obj_set_size(key_obj, 250, 44);
    lv_obj_align_to(key_obj,name_obj, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_obj_set_scrollbar_mode(key_obj, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能
    /* 创建密码输入框 */
    password_input = lv_textarea_create(window_obj);
    lv_textarea_set_placeholder_text(password_input,"password");//提示文本
    lv_obj_set_scrollbar_mode(password_input, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能
    lv_textarea_set_password_mode(password_input, true);  //密码模式
    lv_textarea_set_accepted_chars(password_input, "0123456789"); //字符白名单
    lv_obj_set_size(password_input, 210, 44);
    lv_textarea_set_one_line(password_input, true);                    //限制为1行高度
    lv_obj_set_style_border_opa(password_input,0,0);
    lv_obj_align_to(password_input,key_obj, LV_ALIGN_CENTER,19, 0);
    lv_obj_add_event_cb(password_input, password_event_cb, LV_EVENT_ALL, NULL); //用户名输入框回调
    /*创建密码键盘*/
    password_keyboard =lv_keyboard_create(lv_scr_act());
    lv_obj_set_size(password_keyboard,600,200);
    lv_obj_set_style_radius(password_keyboard,20,0);
    lv_keyboard_set_textarea(password_keyboard,password_input);
    lv_keyboard_set_mode(password_keyboard,LV_KEYBOARD_MODE_NUMBER); //数字键盘模式
    lv_obj_add_event_cb(password_keyboard, keyboard_event_cb, LV_EVENT_READY, NULL);
    lv_obj_add_flag(password_keyboard,LV_OBJ_FLAG_HIDDEN);//默认隐藏
    /*密码图标框*/
    lv_obj_t *password_obj = lv_obj_create(window_obj);
    lv_obj_set_size(password_obj,44,44);
    lv_obj_set_style_bg_color(password_obj,lv_color_hex(0x696969),0);
    lv_obj_align_to(password_obj,password_input,LV_ALIGN_OUT_LEFT_MID,0,0);
    lv_obj_set_scrollbar_mode(password_obj, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能
    /*密码图标*/
    lv_obj_t * password_label = lv_label_create(window_obj);
    lv_obj_set_style_text_font(password_label,&user_fontawesome,0);
    lv_label_set_text(password_label, FONTAWESOME_SYMBOL_KEY);
    lv_obj_set_style_text_color(password_label,lv_color_hex(0xffffff),0);
    lv_obj_align_to(password_label,password_obj,LV_ALIGN_CENTER,0,0);
//--------------------------------------------------------------------------
    /* 创建登录按钮 */
    lv_obj_t * login_btn = lv_btn_create(window_obj);
    lv_obj_set_size(login_btn, 255, 44);
    lv_obj_align_to(login_btn,key_obj, LV_ALIGN_OUT_BOTTOM_LEFT, -3, 20);
     /*创建按钮标签 */
    lv_obj_t * login_label = lv_label_create(login_btn);
    lv_label_set_text(login_label, "登录");
    lv_obj_center(login_label);
    lv_obj_set_style_text_font(login_label,&user_login_name_12,0);
    lv_obj_set_size(login_btn, 120, 44);
    /* 设置按钮事件 */
    lv_obj_add_event_cb(login_btn, login_btn_event_cb, LV_EVENT_CLICKED, NULL);
    // 创造管理员登录按钮
    lv_obj_t * user_btn = lv_btn_create(window_obj);
    lv_obj_set_size(user_btn, 100, 44);
    lv_obj_align_to(user_btn,name_obj, LV_ALIGN_OUT_TOP_MID, 0, 0);
    /*创建管理员登录标签 */
    lv_obj_t * user_label = lv_label_create(user_btn);
    lv_label_set_text(user_label, "用户/管理员");
    lv_obj_set_style_text_font(user_label,&lv_font_source_han_sans_bold_14,0);
    lv_obj_center(user_label);
    lv_obj_set_style_text_color(user_label, lv_color_hex(0xffffff), 0); // 设置字体颜色为白色
    /* 设置管理员按钮事件 */


    /* 创建注册按钮 */
    lv_obj_t * register_btn = lv_btn_create(window_obj);
    lv_obj_set_size(register_btn, 100, 44);
    lv_obj_align_to(register_btn,key_obj, LV_ALIGN_OUT_BOTTOM_RIGHT, 3, 20);
     /*创建注册标签 */
    lv_obj_t * register_label = lv_label_create(register_btn);
    lv_label_set_text(register_label, "注册");
    lv_obj_center(register_label);
    lv_obj_set_style_text_font(register_label,&lv_font_source_han_sans_bold_14,0);
    lv_obj_set_style_text_color(register_label, lv_color_hex(0xffffff), 0); // 设置字体颜色为白色
    /* 设置注册事件 */
    lv_obj_add_event_cb(register_btn, register_btn_event_cb, LV_EVENT_CLICKED, NULL);


    
}


static void lv_messagebox_create(void)
{
    static const char  *tips_txt={"  密码错误，请重新输入！"};

    msgbox = lv_msgbox_create(lv_scr_act(),NULL,tips_txt,NULL,true );
    lv_obj_set_style_text_font(msgbox,&user_login_name_12,0);
    lv_obj_set_size(msgbox,300,150);
    lv_obj_set_style_bg_opa(msgbox,200,0);
    lv_obj_align(msgbox,LV_ALIGN_CENTER,0,50);

    lv_obj_set_style_pad_top(msgbox,15,LV_STATE_DEFAULT);  //顶部填充
    lv_obj_t *close_btnt = lv_msgbox_get_close_btn(msgbox); //获取到关闭图标
    lv_obj_set_style_text_color(close_btnt,lv_color_hex(0x696969),LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(close_btnt,&lv_font_montserrat_20,0);
    lv_obj_set_style_pad_top(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_opa(close_btnt,0,LV_STATE_DEFAULT);


    lv_obj_t* msgbox_content = lv_msgbox_get_content(msgbox); //获取到主体
//    lv_obj_set_style_text_color(msgbox_content,lv_color_hex(0xFF0000),LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(msgbox_content,lv_color_hex(0x808080),LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(msgbox_content,10,LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(msgbox_content,5,0);
    lv_obj_set_style_text_line_space(msgbox_content,10,0);

    lv_obj_add_flag(msgbox,LV_OBJ_FLAG_HIDDEN);                 //默认隐藏

}



void mygui(void)
{
    
    lv_login_window_demo();
}

